package com.schooldate.model.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.schooldate.base.BaseAction;
import com.schooldate.model.entity.Department;
import com.schooldate.model.entity.ExamSource;
import com.schooldate.model.entity.School;
import com.schooldate.model.entity.UserInfo;
import com.schooldate.util.CommomUtils;
import com.schooldate.util.SimpleJsonFilter;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created by etund on 2015/4/25 0025.
 */
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class ExamSourceAction extends BaseAction<ExamSource> {

    private String userId;
    private String schoolInfoId;
    private String departmentId;

    /*关键词*/
    private String keyWord;

    public void uploadSource() throws Exception {
        if (file!=null&&file.length>0){
            uploadFileReturnPath("examSource");
            model.setSourcePath(fileFullPath);
            model.setName(documentName);
            String sourcePath =   ServletActionContext.getServletContext().getRealPath("/examSource");
            sourcePath = sourcePath.split("examSource")[0];
            for (String str:model.getSourcePathes()){
                model.setTitle(CommomUtils.getWordInfomation("title",sourcePath+str));
                model.setContent(CommomUtils.getWordInfomation("content",sourcePath+str));
            }
            this.examSourceService.save(model);
            model.setExamSourceId(model.getId());
            this.examSourceService.update(model);
//           用户鲜花数加2
            UserInfo userInfo = this.userInfoService.getById(Long.parseLong(userId));
            userInfo.setFlowerSendLeft(userInfo.getFlowerSendLeft()+2);
            this.userInfoService.update(userInfo);
            object.put("code",1);
            result = JSON.toJSONString(object);
            renderJson(result);
            return;
        }
        object.put("code",0);
        result = JSON.toJSONString(object);
        renderJson(result);
        return;
    }

    public void searchSource() throws Exception{
        if (keyWord!=null&&!"".equals(keyWord.trim())){
            School school = this.schoolService.getBySchoolName(schoolInfoId);
            Department department = this.departmentService.getByName(departmentId);
            if (school==null||department==null){
                return;
            }
            examSources = this.examSourceService.SearchExam(keyWord,school.getId(),department.getId());
            SimpleJsonFilter filter = new SimpleJsonFilter("searchSource.do");
            object.put("examSources", examSources);
            object.put("code",1);
            result = JSON.toJSONString(object,filter, SerializerFeature.DisableCircularReferenceDetect);
            renderJson(result);
            return;
        }
        object.put("code",0);
        result = JSON.toJSONString(object);
        renderJson(result);
    }


    public void downLoadSource() throws Exception{
        UserInfo userInfo = this.userInfoService.getById(Long.parseLong(userId));

    }


    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public String getSchoolInfoId() {
        return schoolInfoId;
    }

    public void setSchoolInfoId(String schoolInfoId) {
        this.schoolInfoId = schoolInfoId;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }
}
